package com.zuilapsa.jmc.actions;

import bsh.Interpreter;

import com.zuilapsa.jmc.Configuration;
import com.zuilapsa.jmc.JmcException;
import com.zuilapsa.jmc.model.Level;
import com.zuilapsa.jmc.userInterfaces.UserInterfaceSession;

public class ActionBean extends ActionNull implements ActionInterface {
	String name;
	String script;
	
	public ActionBean(String name, String script) {
		this.name = name;
		try {
			this.script = Configuration.getInstance().getRootConfigElement().selectSingleNode("/level[@name='root']/level[@name='config']/level[@name='jmc']/property[@name='bshdir']/@value").getStringValue()+"/"+script;
		} catch (JmcException e) {
			System.err.println("Unable to read /root/config/jmc/bshdir :"+e.getMessage());
			this.script = script;
		}
	}
	public void execute(UserInterfaceSession interfaceSession, String[] args)
			throws JmcException {
		Interpreter i = new Interpreter();
		try {
			i.set("config", Level.getRootInstance().getConfigElement());
			i.set("model", Level.getRootInstance());
			i.set("session", interfaceSession);
			i.set("script", script);
			i.set("name", name);
			
			i.source(script);
		} catch (Exception e) {
			System.err.println("error executiong script "+script+": "+e);
		}	
	}

	public String getHelpString() {
		return "No help available for "+name;
	}

}
